package linkedlist;

public class MyLinkedList implements IList{
    static class ListNode{
        public int val;
        public ListNode prev;
        public ListNode next;

        public ListNode(int val){
            this.val = val;
        }


    }
    public ListNode head;
    public ListNode tail;
    @Override
    public void addFirst(int data) {
        ListNode node = new ListNode(data);
        if (head == null){
            head = node;
            tail = node;
        }else{
            node.next = head;
            head.prev = node;
            head = node;
        }



    }

    @Override
    public void addLast(int data) {
        ListNode node = new ListNode(data);
        if (head == null){
            head = node;
            tail = node;
        }else{
            node.prev = tail;
            tail.next = node;
            tail = node;
        }


    }

    @Override
    public void addIndex(int index, int data) {
        int len = size();
        //默认index合法
        if (index < 0 || index > len){
            System.out.println("index不合法！");
            return;
        }
        if (index == 0){
            addFirst(data);
            return;
        }
        if (index == len){
            addLast(data);
            return;
        }
        ListNode node = new ListNode(data);
        ListNode tmp = head;
        while(index != 0){
            tmp = tmp.next;
            index--;
        }
        node.next = tmp;
        tmp.prev.next = tmp;
        node.prev = tmp.prev;
        tmp.prev = node;
        return;


    }

    @Override
    public boolean contains(int key) {
        ListNode tmp = head;
        while(tmp != null){
            if (tmp.val == key){
                return true;
            }
            tmp = tmp.next;
        }
        return false;
    }

    @Override
    public void remove(int key) {
        ListNode tmp = head;
        while(tmp != null){
            if(tmp.val == key){
                if (tmp == head){
                    head = head.next;
                    if (head == null){
                        tail = null;
                    }else{
                        head.prev = null;
                    }

                }else{
                    tmp.prev.next = tmp.next;
                    if(tmp.next == null){
                        tail = tail.prev;
                    }else{
                        tmp.next.prev = tmp.prev;
                    }
                }
                return;
            }else{
                tmp = tmp.next;

            }


        }

    }

    @Override
    public void removeAllKey(int key) {
        ListNode tmp = head;
        while(tmp != null){
            if(tmp.val == key){
                if (tmp == head){
                    head = head.next;
                    if (head != null){
                        head.prev = null;
                    }else{
                        tail = null;
                    }

                }else{
                    tmp.prev.next = tmp.next;
                    if(tmp.next == null){
                        tail = tail.prev;
                    }else{
                        tmp.next.prev = tmp.prev;
                    }
                }
            }
            //继续往后走
                tmp = tmp.next;
            }
        }




    @Override
    public int size() {
        ListNode tmp = head;
        int count = 0;
        while(tmp != null){
            count++;
            tmp = tmp.next;
        }
        return count;
    }

    @Override
    public void clear() {
        ListNode cur = head;
         while(cur != null){
             ListNode tmp = cur.next;
             cur.val = 0;
             cur.prev = null;
             cur.next = null;
             cur = tmp;
         }
         head = null;
         tail = null;

    }

    @Override
    public void display() {
        ListNode tmp = head;
        while(tmp != null){
            System.out.print(tmp.val+" ");
            tmp = tmp.next;
        }
        System.out.println();


    }
}
